package cn.icare.cms.impl;

import cn.icare.cms.GuestBookService;
import cn.icare.cms.StringUtil;
import cn.icare.cms.dao.mapper.GuestBookMapper;
import cn.icare.cms.dao.mapper.ItemCatMapper;
import cn.icare.cms.dao.po.GuestBook;
import cn.icare.cms.dao.po.ItemCat;
import cn.icare.cms.model.vo.rest.MsgDatasVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class GuestBookServiceImpl implements GuestBookService {

    @Resource
    private GuestBookMapper guestBookMapper;

    private final Logger logger = LoggerFactory.getLogger(GuestBookServiceImpl.class);

    @Override
    public Integer createOne(GuestBook ro) {

        ro.setCreatetime(new Date());
        Integer result = this.guestBookMapper.insertSelective(ro);
        if (result > 0) {
            return result;
        }
        return 0;
    }

    @Override
    public boolean updateStatus(Integer id, Integer status) {
        GuestBook model = new GuestBook();
        model.setId(id);
        model.setStatus(status);
        Integer result = this.guestBookMapper.updateByPrimaryKeySelective(model);
        return result > 0;
    }

    @Override
    public MsgDatasVO getList(Integer pageNum, Integer pageSize) {

        MsgDatasVO result = new MsgDatasVO();
        try {
            if (pageSize == 0) {

                List<GuestBook> dataList = this.guestBookMapper.selectAll();
                result.setTotal(dataList.size());
                result.setDatas(dataList);

            } else {
                PageHelper.startPage(pageNum, pageSize);
                List<GuestBook> dataList = this.guestBookMapper.selectAll();
                PageInfo pageInfo = new PageInfo(dataList);
                result.setTotal(pageInfo.getTotal());
                result.setDatas(dataList);
            }
        } catch (Exception e) {
            this.logger.error("getList 异常", e);
        }
        return result;
    }
}
